'use strict';
(function (exports) {
    var $ = null;

    var ChiBiLayer = cc.Layer.extendEX({
        getRootNode: function () {
            return this.getChildByName('Scene');
        },

        ctor: function () {
            this._super();
            var that = this;

            var scene = ccs.load(res.PhzChiBiLayer_json);
            this.addChild(scene.node);

            $ = create$(this.getRootNode());

            this.bg = $('root.bg');
            this.back = $('root.bgpanel.back');
            this.chibi = $('root.bgpanel.chibi');
            this.guo = $('root.bgpanel.guo');
            this.chiIndex = -1;
            this.lastChiList = [];
            this.chiIndexArray = [0, 0, 0];
            this.cards = [];

            TouchUtil.setOnclickListener(this.back, function (node) {
                var chi = that.getChildByName('chiList' + that.chiIndex);
                if (chi) {
                    chi.removeFromParent();
                }
                that.chiIndex--;
                if (that.chiIndex < 0) {
                    that.removeAllChildren();
                } else if (that.chiIndex == 0) {
                    that.chibi.setTexture(huzi.resPath+'dynamicRes/' + 'room_chi.png');
                    that.setData(that.chibiData, that.showNo);
                    that.cards = [];
                } else {
                    that.cards.splice(that.cards.length - 3, 3);
                    that.chibi.setTexture(huzi.resPath+'dynamicRes/' + 'room_bi.png');
                    that.showBiCard(that.chiIndex, that.lastChiList, that.cards);
                }
            });
            TouchUtil.setOnclickListener(this.guo, function (node) {
                that.roomParent.userAct(GUO, that.showNo);
            });
            return true;
        },

        setData: function (data, showNo, parent) {
            this.chibiData = data;
            this.showNo = showNo;
            if (parent) {
                this.roomParent = parent;
            }
            for (var i = 0; i < 10; i++) {
                var cardS = this.getChildByName('cardSelect' + i);
                if (cardS) {
                    cardS.removeFromParent();
                }
            }
            for (var i = 0; i < this.chibiData.length; i++) {
                var cardSelect = new CardSelect(this);
                this.addChild(cardSelect);
                cardSelect.setData(this.showNo, this.chibiData[i], this, 0, [], i);
                cardSelect.setPosition(1280 / 2 + (i - 1) * 100 + 40, 250);
                cardSelect.setName('cardSelect' + i);
            }
            this.lastChiList = this.chibiData;
        },
        showBiCard: function (level, chiList, cards, index) {
            this.cards = cards;
            this.chibi.setTexture(huzi.resPath+'dynamicRes/' + 'room_bi.png');
            var rowIndex = level;
            for (var i = 0; i < 10; i++) {
                var cardS = this.getChildByName('cardSelect' + i);
                if (cardS && cc.sys.isObjectValid(cardS)) {
                    cardS.removeFromParent(true);
                }
            }
            for (var i = 0; i < chiList.length; i++) {
                var cardSelect = new CardSelect(this);
                this.addChild(cardSelect);
                cardSelect.setData(this.showNo, chiList[i], this, level, cards, i);
                cardSelect.setPosition(1280 / 2 + (i - 1) * 100 + 40, 250);
                cardSelect.setName('cardSelect' + i);
            }
            this.addChiLayer(cards, level);
            if (index != undefined) {
                this.chiIndexArray[level] = index;
            }
            if (level == 1) {
                this.lastChiList = this.chibiData[this.chiIndexArray[1]][3];
            }
        },
        addChiLayer: function (chiArray, index) {
            for (var i = 0; i < 10; i++) {
                var cardS = this.getChildByName('chiList' + i);
                if (cardS) {
                    cardS.removeFromParent();
                }
            }
            var hang = Math.floor((chiArray.length + 2) / 3);
            for (var i = 0; i < hang; i++) {
                var chinum = [chiArray[i * 3], chiArray[i * 3 + 1], chiArray[i * 3 + 2]];
                var cardSelect = new CardSelect(this);
                this.addChild(cardSelect);
                cardSelect.setChiData(this.showNo, chinum);
                cardSelect.setPosition((i) * 70, 250);
                cardSelect.setScale(0.8);
                // cardSelect.setTouchEnableStatus(false);
                cardSelect.setName('chiList' + (i + 1));
            }
            this.chiIndex = index;
        },
        sendChi: function (cards) {
            // console.log(cards);
            if (cards != undefined && cards.length > 0) {
                this.roomParent.userAct(CHI, this.showNo, false, cards);
                if (!this && cc.sys.isObjectValid(this)) {
                    this.removeFromParent();
                }
            }
        },

    });
    exports.ChiBiLayer = ChiBiLayer;
})(window);